package com.hjl.spring.servlet; /**
 * @author ServivE
 * @className ${NAME}
 * @description ${description}
 * @version 0.1
 * @date 2024/8/8 14:25
 * @since jdk11
 */

import com.alibaba.fastjson.JSON;
import com.hjl.spring.controller.EmpController;
import com.hjl.spring.mapper.EmpMapper;
import com.hjl.spring.pojo.Emp;
import com.hjl.spring.service.EmpService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
@WebServlet(name = "EmpInfoServlet", value = "/empInfo")
public class EmpInfoServlet extends HttpServlet {
    @Autowired
//    private EmpMapper empMapper;
    private EmpController empController;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


       /* SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        //通过代理模式创建EmpMapper接口的代理实现类对象
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        //调用EmpMapper接口中的方法，就可以根据EmpMapper的全类名匹配元素文件
        // ，通过调用的方法名匹配映射文件中的SQL标签，并执行标签中的SQL语句
        List<Emp> allEmps = mapper.getAllEmps();*/
        //省去了工厂创建sqlSession的过程，因为在配置文件里已经配置mybatis工厂
//        List<Emp> allEmps = empMapper.getAllEmps();
        List<Emp> emps = empController.soutAllEmps();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
      /*  for (Emp e : allEmps) {
            System.out.println(e.getEid() + "  " + e.getEname());
        }*/
        out.println(JSON.toJSONString(emps));
    }
}
